#!/usr/bin/env node

const http = require('https'),
      log  = console.log,
      url  = require('url'),
      city = process.argv[2] || 石家庄,
      addr = 'https://api.66mz8.com/api/weather.php?location' + city;
log(addr);

//注意怎么转码

let  opt =(ad)=>{
  let p =url.parse(ad);
  p.headers={
    'user-agent':'curl/7.29.0'
  };
  return p;
};

get(opt(addr));

function get(options){
  http.get(options,(res)=>{
    log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
    log(res.headers);
    log();
    if(res.statusCode>=300 && res.statusCode<400){
      get(opt(res.headers.location));
    }else{
      res.pipe(process.stdout);
    }
  });
  
}
